<?php
declare (strict_types = 1);

namespace app;

use think\App;
use think\exception\ValidateException;
use think\Response;
use think\Validate;
use think\Request;
use think\helper\Str;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * @var array|mixed
     */
    public mixed $page;
    /**
     * @var array|mixed
     */
    public mixed $pageSize;
    /**
     * Request实例
     *
     * @var Request
     */
    protected Request $request;

    /**
     * 应用实例
     * @var App
     */
    protected App $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected bool $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected array $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app      = $app;
        $this->request  = $this->app->request;
        $this->page     = $this->request->param('page/d' , 1);
        $this->pageSize = $this->request->param('pageSize/d' , 10);

        // 控制器初始化
        $this->initialize();
    }

    /**
     * 初始化
     * @return void
     */
    protected function initialize(): void
    {
        $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
    }

    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data , string|array $validate , array $message = [] , bool $batch = false)
    : bool|array|string {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate , '.')) {
                // 支持场景
                [
                    $validate ,
                    $scene,
                ] = explode('.' , $validate);
            }
            $class = Str::contains($validate , '\\') ? $validate : $this->app->parseClass('validate' , $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch();
        }

        return $v->failException()->check($data);
    }

    /**
     * 验证数据
     *
     * @access protected
     *
     * @param string $msg 提示信息
     * @param array  $data 数据
     * @param int    $code 错误码
     *
     * @return Response
     */
    public function success(string $msg = 'success', array $data = [], int $code = 200): Response
    {
        return json([ 'code' => $code , 'msg'  => $msg , 'data' => $data ]);
    }

    /**
     * @param string $msg 提示信息
     * @param array  $data 数据
     * @param int    $code 错误码
     *
     * @return Response
     */
    public function error(string $msg = 'fail' , array $data = [], int $code = 500): Response
    {
        return json([ 'code' => $code , 'msg'  => $msg , 'data' => $data]);
    }

}
